﻿using System.Text;

namespace YunQue.Core.Common.Helpers
{
    /// <summary>
    /// http客户端帮助类
    /// </summary>
    public class HttpCllientHelper
    {
        private static readonly HttpClient _httpClient = new HttpClient();

        /// <summary>
        /// 设置请求头
        /// </summary>
        /// <param name="headers"></param>
        private static void SetHeaders(Dictionary<string, string> headers)
        {
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    if (_httpClient.DefaultRequestHeaders.Contains(header.Key))
                    {
                        _httpClient.DefaultRequestHeaders.Remove(header.Key);
                    }
                    _httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
            }
        }
        /// <summary>
        /// 设置超时时间
        /// </summary>
        /// <param name="timeout"></param>
        private static void SetTimeout(TimeSpan? timeout)
        {
            if (timeout == null)
            {
                timeout = TimeSpan.FromSeconds(30);
            }
            _httpClient.Timeout = timeout.Value;
        }

        /// <summary>
        /// Get 请求 有返回值
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="url"></param>
        /// <param name="timeout"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static async Task<TResult> GetAsync<TResult>(string url,TimeSpan? timeout=null,Dictionary<string,string> headers=null)
        {
            SetTimeout(timeout);
            SetHeaders(headers);
            var response = await _httpClient.GetAsync(url).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();
            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<TResult>(content);
        }
        /// <summary>
        /// Get 请求 无返回值
        /// </summary>
        /// <param name="url"></param>
        /// <param name="timeout"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static async Task GetAsync(string url, TimeSpan? timeout = null, Dictionary<string, string> headers = null)
        {
            SetTimeout(timeout);
            SetHeaders(headers);
            var response = await _httpClient.GetAsync(url).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();
        }
        /// <summary>
        /// Post 请求 有返回值
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <typeparam name="TRequest"></typeparam>
        /// <param name="request"></param>
        /// <param name="url"></param>
        /// <param name="timeout"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static async Task<TResult> PostAsync<TResult,TRequest>(TRequest request,string url, TimeSpan? timeout = null, Dictionary<string, string> headers = null)
        {
            SetTimeout(timeout);
            SetHeaders(headers);
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(request);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await _httpClient.PostAsync(url, content).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();
            var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<TResult>(responseContent);
        }
        /// <summary>
        /// Post 请求 无返回值
        /// </summary>
        /// <typeparam name="TRequest"></typeparam>
        /// <param name="request"></param>
        /// <param name="url"></param>
        /// <param name="timeout"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static async Task PostAsync<TRequest>(TRequest request, string url, TimeSpan? timeout = null, Dictionary<string, string> headers = null)
        {
            SetTimeout(timeout);
            SetHeaders(headers);
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(request);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await _httpClient.PostAsync(url, content).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();
        }


    }
}
